package com.ews.onlineexchange.controller;

import java.io.FileInputStream;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;

import org.activiti.engine.task.Task;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.ews.onlineexchange.controller.activitii.TodoTaskList;
import com.ews.onlineexchange.model.ServiceSupplier;
import com.ews.onlineexchange.model.assess.Dict;



@Named
public class BpmnTaskExporter implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	static DateFormat df;
	static {
		df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	}
	@Inject
	EntityManager		entityManager;
	@Inject
	TodoTaskList todoTaskList;
	private static final String[] FIELD_NAMES= {		
			"name",	"serviceSupplier.cnname","dict.title","createTime"
		};

	public XSSFWorkbook export(String processKey,String dictId) {
		XSSFWorkbook workbook = null;
		try {
			String template = BpmnTaskExporter.class.getClassLoader().getResource("audit.xlsx").getPath();			
        	FileInputStream fis=new FileInputStream(template);
	        workbook=new XSSFWorkbook(fis);
	        XSSFSheet sheet = (XSSFSheet)workbook.getSheetAt(0);
	        todoTaskList.setProcessKey(processKey);
	        todoTaskList.setMaxResults(null);
	        todoTaskList.setDictId(dictId);
			List<Task> resultList = todoTaskList.getResultList();
			List<CellStyle> styleList = new ArrayList<CellStyle>();
			int index = 0;
			for (Task task : resultList) {
				
				Row dataRow = null ;
				if(index == 0)dataRow = sheet.getRow(1 + index);
				else dataRow = sheet.createRow(1 + index);
				for (int j = 0; j < FIELD_NAMES.length; j++) {
					Cell cell = null;
					if(index == 0) {
						cell = dataRow.getCell(j);
						CellStyle cellStyle = cell.getCellStyle();
						cell.setCellStyle(cellStyle);
						styleList.add(cellStyle);
					}else {
						cell = dataRow.createCell(j);
						cell.setCellStyle(styleList.get(j));
					}
					String fieldValue = "";
					String fieldName = FIELD_NAMES[j];
					Map<String, Object> processVariables = task.getProcessVariables();
					if(fieldName.equals("name")) fieldValue =BeanUtils.getProperty(task, fieldName);
					if(fieldName.equals("createTime")) {
						fieldValue = df.format(task.getCreateTime());
					}
					if(fieldName.equals("serviceSupplier.cnname")) {
						ServiceSupplier suplier = (ServiceSupplier)processVariables.get("serviceSupplier");
						fieldValue = suplier.getCnname();
					}
					if(fieldName.equals("dict.title")) {
						Dict dict = (Dict)processVariables.get("dict");
						fieldValue = dict.getTitle();
					}
					cell.setCellValue(fieldValue);
				}
				index++;
			}
			return workbook;
//			return WorkBookUtil.getSteamedContentByWorkBook(fileName,workbook);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
	}
	
}
